﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChessOnline
{
    public enum ChessType
    {
        None = 0,

        WhiteKing,
        WhiteQueen,
        WhiteBishop,
        WhiteRook,
        WhiteKnight,
        WhitePawn,

        BlackKing,
        BlackQueen,
        BlackBishop,
        BlackRook,
        BlackKnight,
        BlackPawn
    };

    public class GameLogic
    {
        GameBoard gBoard = new GameBoard();

        public List<GameCell> GetCells()
        {
            return gBoard.GetCells();
        }

        public List<Coordinate> TryPickChess(string p, Coordinate coord, ChessColor active_chess_color)
        {
            List<Coordinate> paths = new List<Coordinate>();
            Path pathBuilder = new Path();

            if (active_chess_color == ChessColor.White)
            {
                Path Check = new Path();
                List<List<Coordinate>> CheckedPaths = Check.BuildCheckPass(active_chess_color, gBoard);
                if (CheckedPaths.Count == 0)
                    switch (gBoard.GetDicCells()[coord].FigureType)
                    {
                        case ChessType.WhiteBishop:
                            pathBuilder.PathBishop(coord, ChessColor.White, gBoard);
                            break;
                        case ChessType.WhiteKing:
                            pathBuilder.PathKing(coord, ChessColor.White, gBoard);
                            break;
                        case ChessType.WhiteKnight:
                            pathBuilder.PathKnight(coord, ChessColor.White, gBoard);
                            break;
                        case ChessType.WhitePawn:
                            pathBuilder.PathPawn(coord, ChessColor.White, gBoard);
                            break;
                        case ChessType.WhiteQueen:
                            pathBuilder.PathQueen(coord, ChessColor.White, gBoard);
                            break;
                        case ChessType.WhiteRook:
                            pathBuilder.PathRook(coord, ChessColor.White, gBoard);
                            break;
                    } 
                else
                    switch (gBoard.GetDicCells()[coord].FigureType)
                    {
                        case ChessType.WhiteBishop:
                            pathBuilder.CheckedPathBishop(coord, ChessColor.White, gBoard, CheckedPaths);
                            break;
                        case ChessType.WhiteKing:
                            pathBuilder.CheckedPathKing(coord, ChessColor.White, gBoard, CheckedPaths);
                            break;
                        case ChessType.WhiteKnight:
                            pathBuilder.CheckedPathKnight(coord, ChessColor.White, gBoard, CheckedPaths);
                            break;
                        case ChessType.WhitePawn:
                            pathBuilder.CheckedPathPawn(coord, ChessColor.White, gBoard, CheckedPaths);
                            break;
                        case ChessType.WhiteQueen:
                            pathBuilder.CheckedPathQueen(coord, ChessColor.White, gBoard, CheckedPaths);
                            break;
                        case ChessType.WhiteRook:
                            pathBuilder.CheckedPathRook(coord, ChessColor.White, gBoard, CheckedPaths);
                            break;
                    }
            }

            if (active_chess_color == ChessColor.Black)
            {
                Path Check = new Path();
                List<List<Coordinate>> CheckedPaths = Check.BuildCheckPass(active_chess_color, gBoard);
                if (CheckedPaths.Count == 0)
                    switch (gBoard.GetDicCells()[coord].FigureType)
                    {
                        case ChessType.BlackBishop:
                            pathBuilder.PathBishop(coord, ChessColor.Black, gBoard);
                            break;
                        case ChessType.BlackKing:
                            pathBuilder.PathKing(coord, ChessColor.Black, gBoard);
                            break;
                        case ChessType.BlackKnight:
                            pathBuilder.PathKnight(coord, ChessColor.Black, gBoard);
                            break;
                        case ChessType.BlackPawn:
                            pathBuilder.PathPawn(coord, ChessColor.Black, gBoard);
                            break;
                        case ChessType.BlackQueen:
                            pathBuilder.PathQueen(coord, ChessColor.Black, gBoard);
                            break;
                        case ChessType.BlackRook:
                            pathBuilder.PathRook(coord, ChessColor.Black, gBoard);
                            break;
                    }  
                else
                    switch (gBoard.GetDicCells()[coord].FigureType)
                    {
                        case ChessType.BlackBishop:
                            pathBuilder.CheckedPathBishop(coord, ChessColor.Black, gBoard, CheckedPaths);
                            break;
                        case ChessType.BlackKing:
                            pathBuilder.CheckedPathKing(coord, ChessColor.Black, gBoard, CheckedPaths);
                            break;
                        case ChessType.BlackKnight:
                            pathBuilder.CheckedPathKnight(coord, ChessColor.Black, gBoard, CheckedPaths);
                            break;
                        case ChessType.BlackPawn:
                            pathBuilder.CheckedPathPawn(coord, ChessColor.Black, gBoard, CheckedPaths);
                            break;
                        case ChessType.BlackQueen:
                            pathBuilder.CheckedPathQueen(coord, ChessColor.Black, gBoard, CheckedPaths);
                            break;
                        case ChessType.BlackRook:
                            pathBuilder.CheckedPathRook(coord, ChessColor.Black, gBoard, CheckedPaths);
                            break;
                    }
            }

            List<Coordinate> result = pathBuilder.path;
            if (result.Count > 0)
            {// Добавим в выделение запрошенную координату тоже
                result.Add(coord);
            }
            return result;
        }

        public GameLogic()
        {

        }

        public string MoveChess(string p, Coordinate old_coord, Coordinate new_coord, ChessColor chessColor)
        {
            List<Coordinate> list = TryPickChess(p, old_coord, chessColor);
            string result = "false";

            if (list.Contains(new_coord))
            {// Можно передвинуть фигуру!
                // двигаем фигуру
                result = "true";
                Dictionary<Coordinate, GameCell> cells = gBoard.GetDicCells();
                //if (cells[new_coord].FigureType == ChessType.BlackKing || cells[new_coord].FigureType == ChessType.WhiteKing)
                //{
                //    result = "victory";
                //}

                cells[new_coord].FigureType = cells[old_coord].FigureType;
                cells[old_coord].FigureType = ChessType.None;

                // если фигура - пешка и она дошла до последней линии
                if (cells[new_coord].FigureType == ChessType.BlackPawn && new_coord.Number == 1)
                {
                    cells[new_coord].FigureType = ChessType.BlackQueen;
                }

                if (cells[new_coord].FigureType == ChessType.WhitePawn && new_coord.Number == 8)
                {
                    cells[new_coord].FigureType = ChessType.WhiteQueen;
                }
              
                gBoard.SetCellsFromDictionary(cells);

                //Определение состояния мата
                Path IsStaleMate = new Path();
                if ( IsStaleMate.IsStaleMate(chessColor, gBoard))
                {
                    result = "victory";
                }
            }

            return result;
        }
    }
}
